package nml.webmail;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class china_com_cn extends WebMail {

	public void send(String uid, String password, String to, String subject, String content) throws Exception {

		try {
			String url = "http://mail.china.com.cn/cgi-bin/index.cgi";
			dr.get(url);


			loadJQuery();
			executeScript(" $('form[name=FormData]').submit(function () {return true;})");
			
			executeScript(" $('form[name=FormData]').attr('onsubmit',' return true ')");
			
			

			dr.findElement(By.name("username")).sendKeys(uid);
			dr.findElement(By.name("password")).sendKeys(password);

			dr.findElement(By.name("FormData")).submit();

			waitWebElement(By.name("menu"));
			dr.switchTo().frame("menu");

			waitWebElement(By.className("bt_style"));
			
			
			dr.findElements(By.className("bt_style")).get(1).click();

			
			
			
			dr.switchTo().defaultContent().switchTo().frame("quark_main");

			waitWebElement(By.name("sign_check"));
			
			loadJQuery();
			String js = loadJS();

			content = StringUtils.remove(content, '\r');
			content = StringUtils.remove(content, '\n');
			content = StringEscapeUtils.escapeJavaScript(content);

			js = StringUtils.replace(js, "$content", content);
			js = StringUtils.replace(js, "$subject", subject);
			js = StringUtils.replace(js, "$to", to);

			executeScript(js);
			
			
			//dr.switchTo().defaultContent().switchTo().frame("Composition");
			
			//executeScript( "document.body.innerHTML =  '"+content+"' " );
			

			//dr.switchTo().defaultContent().switchTo().frame("quark_main");
			
			
			
			
			

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			dr.quit();
		}
	}

	public static void main(String[] args) {
		try {
			String uid = "aa1798654";
			String password = "qq3131798654";
			String to = "o21x@qq.com";
			String subject = "nice chek in";
			String content = FileUtils.readFileToString(new File("a.txt"));

			new china_com_cn().send(uid, password, to, subject, content);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
